package com.baf.remote;
import com.baf.db.*;
import android.database.sqlite.*;
import android.database.*;
import com.baf.xbmcapi.XBMCInterface;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.SimpleCursorAdapter;
import android.widget.EditText;
import android.widget.TextView;
import android.media.MediaPlayer;
import android.util.Log;
import android.net.Uri;
import org.json.*;
import android.widget.* ;
import java.io.*;

public class DictionaryActivity extends ListActivity {
 static final String[] COUNTRIES = new String[] {
    "Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra",
    "Angola", "Anguilla", "Antarctica", "Antigua and Barbuda", "Argentina",
    "Yemen", "Yugoslavia", "Zambia", "Zimbabwe"
  };
 

/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_example);
//
//
//
//
//	First try to load data if its not already loaded. This is a one time activity per device
	DataBaseHelper myDbHelper ;
        myDbHelper = new DataBaseHelper(this);
 
        try {
 
        	myDbHelper.createDataBase();
 
 	} catch (IOException ioe) {
 
 		throw new Error("Unable to create database");
 
 	}
 
 	try {
 
 		myDbHelper.openDataBase();
 
 	}catch(SQLException sqle){
 
 		throw sqle;
 
 	}
	DictionaryOpenHelper doh;
	doh = new DictionaryOpenHelper (this);
	SQLiteDatabase mydb = doh.getReadableDatabase();
	Cursor v = mydb.query("ordbog", null, "dansk like '"+Preferences.getAlphabet() + "%'" + " and difficulty >= " +Preferences.getDifficulty(), null,null, null,null);
//	String sarr[] = new String [v.getCount()];
//	int i=0;
//	while (v.moveToNext())
//	{
//		sarr[i++] = v.getString(0)+ " --- " +v.getString(1);
//	}
	String [] columns = new String [] {"dansk", "engelsk"};
	int [] to = new int [] {R.id.dansk_entry, R.id.engelsk_entry} ;
	setListAdapter(new SimpleCursorAdapter( this, R.layout.list_example_entry  , v, columns , to));
     //   setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item,sarr ));

  ListView lv = getListView();
  //lv.setTextFilterEnabled(true);()

  lv.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) {
	CharSequence englishtext = ((TextView) view.findViewById(R.id.engelsk_entry)).getText();
	String danishtext = ((TextView) view.findViewById(R.id.dansk_entry)).getText().toString();
	String dentext= Uri.encode(danishtext);
	Log.v("Prefs", "Printing Pref values "+ Preferences.getPronounce() + Preferences.getMeaning());
	if (Preferences.getPronounce())
	{	
		MediaPlayer mp =MediaPlayer.create(getApplicationContext(),Uri.parse("http://translate.google.com/translate_tts?tl=da&q="+dentext));
		mp.start();
	}
	if (Preferences.getMeaning())
	{
      		Toast.makeText(getApplicationContext(),englishtext ,Toast.LENGTH_LONG).show();
	}
    }
  });

    }
    

}

